package com.opd.soeghtool;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class BtnFuncSetting extends Activity {
    final String TAG = "SOEFUNC";
    GlobalVar mGlobalVar;
    ArrayList spinner_lst;
    Spinner spinner_combin_btn_sel,spinner_combin_func_sel,spinner_combin_ed_btn;
    Button btn_func_combin_add,btn_func_combin_save,btn_func_combin_reset;
    TextView tv_func_combin_view;

    RadioButton rb_combin_left_rocker,rb_combin_right_rocker,rb_fpc_gun_quick,rb_fpc_gun_user,rb_func_gun_sub,rb_func_gun_rifle,rb_func_gun_machine;
    EditText et_func_btn_dir_time,et_func_pull_gun_thr,et_func_pull_gun_inv,et_func_pull_gun_del_x,et_func_pull_gun_del_y;
    LinearLayout ll_func_combin_btn,ll_func_pull_gun,ll_func_btn_direct,ll_func_gun_user,ll_func_gun_quick;
    ImageView iv_combin_top_back;

    byte g_u8Databuf[];
    byte g_u8tAttrTable[][];
    byte g_u8TempAttrTable[];
    byte g_u8tAutoEventTbl[];
    byte g_u8TempAutoEventTbl[];
    byte g_u8AutoEventTblUsed;
    byte g_u8ActionTblStar=0,g_u8ActionTblLen=0;
    int g_u8ButtonID = 0,g_u8EVKey=0,mRes_Wid=1,mRes_Hei=1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.combin_setting_layout);
        mGlobalVar = (GlobalVar)getApplication();

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(dm);
        mRes_Wid=dm.widthPixels;
        mRes_Hei = dm.heightPixels;

        g_u8Databuf = new byte[Transfer_Con.HID_PACKET_SIZE];
        g_u8tAttrTable =  new byte[mGlobalVar.getDeviceMaxButton()][3];
        g_u8TempAttrTable =  new byte[3];
        g_u8tAutoEventTbl = new byte[Transfer_Con.MAX_AUTOEVENT_LENGTH];
        g_u8TempAutoEventTbl = new byte[Transfer_Con.MAX_AUTOEVENT_LENGTH];
        g_u8AutoEventTblUsed = 0;

        ll_func_combin_btn = (LinearLayout)findViewById(R.id.ll_func_combin_btn);
        ll_func_pull_gun = (LinearLayout)findViewById(R.id.ll_func_pull_gun);
        ll_func_btn_direct = (LinearLayout)findViewById(R.id.ll_func_btn_direct);
        ll_func_gun_user = (LinearLayout)findViewById(R.id.ll_func_gun_user);
        ll_func_gun_quick = (LinearLayout)findViewById(R.id.ll_func_gun_quick);

        rb_combin_left_rocker = (RadioButton)findViewById(R.id.rb_combin_left_rocker);
        rb_combin_right_rocker = (RadioButton)findViewById(R.id.rb_combin_right_rocker);
        rb_fpc_gun_quick = (RadioButton)findViewById(R.id.rb_fpc_gun_quick);
        rb_fpc_gun_user = (RadioButton)findViewById(R.id.rb_fpc_gun_user);
        rb_func_gun_sub = (RadioButton)findViewById(R.id.rb_func_gun_sub);
        rb_func_gun_rifle = (RadioButton)findViewById(R.id.rb_func_gun_rifle);
        rb_func_gun_machine = (RadioButton)findViewById(R.id.rb_func_gun_machine);


        et_func_btn_dir_time = (EditText)findViewById(R.id.et_func_btn_dir_time);
        et_func_pull_gun_thr = (EditText)findViewById(R.id.et_func_pull_gun_thr);
        et_func_pull_gun_inv = (EditText)findViewById(R.id.et_func_pull_gun_inv);
        et_func_pull_gun_del_x = (EditText)findViewById(R.id.et_func_pull_gun_del_x);
        et_func_pull_gun_del_y = (EditText)findViewById(R.id.et_func_pull_gun_del_y);

        for(int a=0;a<Transfer_Con.MAX_AUTOEVENT_LENGTH;a+=30) {
            if(mGlobalVar.tranGet_Value(Transfer_Con.HID_CMD_GET_AEV_TBL, a, (a+30>=Transfer_Con.MAX_AUTOEVENT_LENGTH)?(Transfer_Con.MAX_AUTOEVENT_LENGTH-a):30, g_u8Databuf))
            {
                System.arraycopy(g_u8Databuf,16,g_u8tAutoEventTbl,a,(a+30>=Transfer_Con.MAX_AUTOEVENT_LENGTH)?(Transfer_Con.MAX_AUTOEVENT_LENGTH-a):30);
            }
        }
        for(int a=0;a<3;a++) {
            if(mGlobalVar.tranGet_Value(Transfer_Con.HID_CMD_GET_KEY_ATTR, a, 0, g_u8Databuf))
            {
                for(int k=0;k<mGlobalVar.getDeviceMaxButton();k++)
                {
                    g_u8tAttrTable[k][a] = g_u8Databuf[16+k];
                }
            }
        }

        for(int k=0;k<mGlobalVar.getDeviceMaxButton();k++)
        {
            if(g_u8tAttrTable[k][0] !=0)
            {
                if(g_u8tAttrTable[k][1]>=g_u8AutoEventTblUsed)
                    g_u8AutoEventTblUsed=(byte)(g_u8tAttrTable[k][1]+g_u8tAttrTable[k][2]);
            }
        }

        tv_func_combin_view = (TextView) findViewById(R.id.tv_func_combin_view);

        spinner_combin_btn_sel = (Spinner)findViewById(R.id.spinner_combin_btn_sel);
        spinner_lst = new ArrayList<String>();
        for (int i = 0; i < mGlobalVar.getDeviceMaxButton() ; i++){
            spinner_lst.add(Transfer_Con.tButtonName[i]);
        }
        ArrayAdapter<String> adapter1 =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,spinner_lst);
        spinner_combin_btn_sel.setAdapter(adapter1);
        g_u8ButtonID = 0;
        spinner_combin_btn_sel.setSelection(0);
        spinner_combin_btn_sel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                g_u8ButtonID = position;
                spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_NOTHING);
                g_u8TempAttrTable[0] = g_u8tAttrTable[g_u8ButtonID][0];
                g_u8TempAttrTable[1] = g_u8tAttrTable[g_u8ButtonID][1];
                g_u8TempAttrTable[2] = g_u8tAttrTable[g_u8ButtonID][2];

                Log.d(TAG,"spinner_combin_btn_sel: pos="+String.valueOf(position));
                    switch((int)g_u8TempAttrTable[0]&Transfer_Con.ATTR_MASK)
                    {
                        case Transfer_Con.bSkillWithDirect:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_SKILL_DIR);
                            break;
                        case Transfer_Con.bFPSPullGunDown:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_PULL_GUN);
                            break;
                        case Transfer_Con.ATTR_MACRO:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_COM_SKILL);
                            break;
                        case Transfer_Con.bSwipe:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_SWIPE);
                            break;
                        case Transfer_Con.bWZRYCancelConjure:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_CANCEL_SKILL);
                            break;
                        case Transfer_Con.ATTR_MACRO_ADV:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_USER_DEF);
                            break;
                        case (byte)Transfer_Con.bCancelAutoEvent:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_CANCEL);
                            break;
                        default:
                            spinner_combin_func_sel.setSelection(Transfer_Con.FUNC_ITEM_NOTHING);
                            break;
                    }

                }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner_combin_func_sel = (Spinner)findViewById(R.id.spinner_combin_func_sel);
        spinner_lst = new ArrayList<String>();
        String strButtonFunctionName[] = getResources().getStringArray(R.array.StringButtonFunction);
        for (int i = 0; i < strButtonFunctionName.length ; i++){
            spinner_lst.add(strButtonFunctionName[i]);
        }
        adapter1 =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,spinner_lst);
        spinner_combin_func_sel.setAdapter(adapter1);
        spinner_combin_func_sel.setSelection(0, true);
        spinner_combin_func_sel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


                switch (position)
                {
                    case Transfer_Con.FUNC_ITEM_SKILL_DIR:
                        g_u8TempAttrTable[0] = Transfer_Con.bSkillWithDirect;
                        break;
                    case Transfer_Con.FUNC_ITEM_PULL_GUN:
                        g_u8TempAttrTable[0] = Transfer_Con.bFPSPullGunDown;
                        break;
                    case Transfer_Con.FUNC_ITEM_COM_SKILL:
                        g_u8TempAttrTable[0] = Transfer_Con.ATTR_MACRO;
                        break;
                    case Transfer_Con.FUNC_ITEM_CANCEL:
                        g_u8TempAttrTable[0] = (byte)Transfer_Con.bCancelAutoEvent;
                        break;
                        default:
                            g_u8TempAttrTable[0] = 0;
                            break;
                }

                Log.d(TAG,"spinner_combin_func_sel: pos="+String.valueOf(position));
                ll_func_combin_btn.setVisibility(View.GONE);
                ll_func_pull_gun.setVisibility(View.GONE);
                ll_func_btn_direct.setVisibility(View.GONE);
                if(position==Transfer_Con.FUNC_ITEM_PULL_GUN)
                {
                    long time;
                    int time1;
                    ll_func_pull_gun.setVisibility(View.VISIBLE);
                    time = (int)(g_u8tAutoEventTbl[g_u8TempAttrTable[1]]&0xFF);
                    et_func_pull_gun_thr.setText(String.valueOf(time*100));
                    time = (int)(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+1]&0xFF);
                    et_func_pull_gun_inv.setText(String.valueOf(time*100));

                    g_u8tAutoEventTbl[g_u8TempAttrTable[1]+4]=16;
                    g_u8tAutoEventTbl[g_u8TempAttrTable[1]+2]=(byte)141;
                    g_u8tAutoEventTbl[g_u8TempAttrTable[1]+3]=(byte)47;

                    time1 = ((int)(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+4]&0xFF)&0x0F);
                    time =   (int)(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+2]&0xFF);

                    time = time1*256 + time;

                    et_func_pull_gun_del_x.setText(String.valueOf((time*mRes_Hei+0x7FFF/2)/0x7FFF));
                    time1 = (int)(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+4]&0xF0);
                    time =  (int)(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+3]&0xFF);

                    time = time1*16 + time;

                    et_func_pull_gun_del_y.setText(String.valueOf((time*mRes_Wid+0x7FFF/2)/0x7FFF));
                }
                else if(position==Transfer_Con.FUNC_ITEM_SKILL_DIR)
                {
                    ll_func_btn_direct.setVisibility(View.VISIBLE);
                    if((g_u8tAutoEventTbl[g_u8TempAttrTable[1]]&0x80)==0x80)
                    {
                        rb_combin_left_rocker.setChecked(true);
                    }
                    else
                    {
                        rb_combin_right_rocker.setChecked(true);
                    }
                    int time = ((int)g_u8tAutoEventTbl[g_u8TempAttrTable[1]]&0x7f);
                    et_func_btn_dir_time.setText(String.valueOf(time*100));
                }
                else if(position==Transfer_Con.FUNC_ITEM_CANCEL)
                {
                    g_u8TempAttrTable[1] = 0;
                    g_u8TempAttrTable[2] = 0;
                }
                else if(position==Transfer_Con.FUNC_ITEM_COM_SKILL){
                    if((int)(g_u8TempAttrTable[0]&0x7F)==Transfer_Con.ATTR_MACRO)
                        parseAutoEvent();
                        g_u8ActionTblLen = 0;
                    ll_func_combin_btn.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner_combin_ed_btn = (Spinner)findViewById(R.id.spinner_combin_ed_btn);
        spinner_lst = new ArrayList<String>();
        for (int i = 0; i < mGlobalVar.getDeviceMaxButton() ; i++){
            spinner_lst.add(Transfer_Con.tButtonName[i]);
        }
        adapter1 =  new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,spinner_lst);
        spinner_combin_ed_btn.setAdapter(adapter1);
        spinner_combin_ed_btn.setSelection(0, true);
        spinner_combin_ed_btn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                g_u8EVKey = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        btn_func_combin_add = (Button)findViewById(R.id.btn_func_combin_add);
        btn_func_combin_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(g_u8ActionTblLen>0) {
                    g_u8TempAutoEventTbl[g_u8ActionTblStar + g_u8ActionTblLen] = 1; // inv time
                    g_u8ActionTblLen++;
                }
                g_u8TempAutoEventTbl[g_u8ActionTblStar+g_u8ActionTblLen] = (byte)(g_u8EVKey|0x80);  // touch
                g_u8ActionTblLen++;
                g_u8TempAutoEventTbl[g_u8ActionTblStar+g_u8ActionTblLen]=2; // inv time
                g_u8ActionTblLen++;
                g_u8TempAutoEventTbl[g_u8ActionTblStar+g_u8ActionTblLen] = (byte)(g_u8EVKey);   // touch release


                g_u8ActionTblLen++;
                String str="";
                for(int a=0;a<g_u8ActionTblLen;a++) {
                    if(a%2==0) {
                        if ((g_u8TempAutoEventTbl[g_u8ActionTblStar + a] & 0x80) == 0x80) {
                            str += Transfer_Con.tButtonName[g_u8TempAutoEventTbl[g_u8ActionTblStar + a] & 0x3F] + "按";
                        } else {
                            str += Transfer_Con.tButtonName[g_u8TempAutoEventTbl[g_u8ActionTblStar + a] & 0x3F] + "松";
                        }
                    }
                    else {
                        if (g_u8TempAutoEventTbl[g_u8ActionTblStar + a ] == 0) {
                            str += ", 同时";
                        } else {
                            str += ", 等" + String.valueOf(g_u8TempAutoEventTbl[g_u8ActionTblStar + a] * 100) + "ms, ";
                        }
                    }
                }

                tv_func_combin_view.setText(str);
            }
        });

        rb_fpc_gun_quick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rb_fpc_gun_quick.isChecked())
                {
                    ll_func_gun_user.setVisibility(View.GONE);
                    ll_func_gun_quick.setVisibility(View.VISIBLE);
                }
            }
        });
        rb_fpc_gun_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rb_fpc_gun_user.isChecked()) {
                    ll_func_gun_quick.setVisibility(View.GONE);
                    ll_func_gun_user.setVisibility(View.VISIBLE);
                }
            }
        });

        iv_combin_top_back = (ImageView) findViewById(R.id.iv_combin_top_back);
        iv_combin_top_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_func_combin_save = (Button)findViewById(R.id.btn_func_combin_save);
        btn_func_combin_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String str_data;
                long tempVal;
                if(spinner_combin_func_sel.getSelectedItemPosition()==Transfer_Con.FUNC_ITEM_SKILL_DIR)    // 联动
                {
                    str_data = et_func_btn_dir_time.getText().toString();
                    if("".equals(str_data)){
                        str_data = "0";
                    }
                    tempVal=Integer.parseInt(str_data);
                    if(tempVal>10000)
                        tempVal = 10000;

                    g_u8TempAutoEventTbl[0] =(byte)(tempVal/100) ;

                    if(rb_combin_left_rocker.isChecked())
                    {
                        g_u8TempAutoEventTbl[0]|=0x80;
                    }
                    g_u8TempAutoEventTbl[1]=(byte)0xff;
                    g_u8TempAttrTable[1] = 0;
                    g_u8TempAttrTable[2] = 2;
                    g_u8ActionTblLen = 2;
                }
                else if(spinner_combin_func_sel.getSelectedItemPosition()==Transfer_Con.FUNC_ITEM_PULL_GUN)   //压枪
                {
                    if(rb_fpc_gun_user.isChecked()) {
                        str_data = et_func_pull_gun_thr.getText().toString();
                        if ("".equals(str_data)) {
                            str_data = "0";
                        }
                        tempVal = Integer.parseInt(str_data);
                        if (tempVal > 25000)
                            tempVal = 25000;
                        g_u8TempAutoEventTbl[0] = (byte) (tempVal / 100);

                        str_data = et_func_pull_gun_inv.getText().toString();
                        if ("".equals(str_data)) {
                            str_data = "0";
                        }
                        tempVal = Integer.parseInt(str_data);
                        if (tempVal > 25000)
                            tempVal = 25000;
                        g_u8TempAutoEventTbl[1] = (byte) (tempVal / 100);

                        str_data = et_func_pull_gun_del_x.getText().toString();
                        if ("".equals(str_data)) {
                            str_data = "0";
                        }
                        tempVal = Integer.parseInt(str_data);
                        tempVal = (tempVal * 0x7FFF + mRes_Hei / 2) / mRes_Hei;

                        g_u8TempAutoEventTbl[2] = (byte) (tempVal & 0xFF);
                        g_u8TempAutoEventTbl[4] = (byte) (tempVal / 0x100);
                        str_data = et_func_pull_gun_del_y.getText().toString();
                        if ("".equals(str_data)) {
                            str_data = "0";
                        }
                        tempVal = Integer.parseInt(str_data);
                        tempVal = ((tempVal * 0x7FFF + mRes_Wid / 2) / mRes_Wid);

                        g_u8TempAutoEventTbl[3] = (byte) (tempVal & 0xFF);
                        g_u8TempAutoEventTbl[4] += (byte) ((tempVal / 0x100) << 4);
                        g_u8TempAutoEventTbl[5] = (byte) (0xff);
                        g_u8TempAttrTable[1] = 0;
                        g_u8TempAttrTable[2] = 6;
                        g_u8ActionTblLen = 6;
                    }
                    else    //if(rb_fpc_gun_quick.isChecked())
                    {
                        if(rb_func_gun_rifle.isChecked())
                        {
                            g_u8TempAutoEventTbl[2] = 100;
                            g_u8TempAutoEventTbl[3] = 0;
                            g_u8TempAutoEventTbl[4] = 0;

                        }
                        else if(rb_func_gun_machine.isChecked())
                        {
                            g_u8TempAutoEventTbl[2] = 60;
                            g_u8TempAutoEventTbl[3] = 0;
                            g_u8TempAutoEventTbl[4] = 0;
                        }
                        else    //if(rb_func_gun_sub.isChecked())
                        {
                            g_u8TempAutoEventTbl[2] = 70;
                            g_u8TempAutoEventTbl[3] = 0;
                            g_u8TempAutoEventTbl[4] = 0;
                        }
                        g_u8TempAutoEventTbl[0]=1;
                        g_u8TempAutoEventTbl[1]=1;
                        g_u8TempAutoEventTbl[5]=(byte)0xff;
                        g_u8TempAttrTable[1] = 0;
                        g_u8TempAttrTable[2] = 6;
                        g_u8ActionTblLen = 6;
                    }
                }
                else if(spinner_combin_func_sel.getSelectedItemPosition()==Transfer_Con.FUNC_ITEM_COM_SKILL)   //连招
                {
                    if(g_u8ActionTblLen!=0) {
                        g_u8TempAutoEventTbl[g_u8ActionTblLen] = (byte) 0xFF;
                        g_u8ActionTblLen++;
                    }
                    g_u8TempAttrTable[2] = g_u8ActionTblLen;
                }
                else if(spinner_combin_func_sel.getSelectedItemPosition()==Transfer_Con.FUNC_ITEM_CANCEL)
                {

                }
                else
                    return;


                if( g_u8tAttrTable[g_u8ButtonID][0]!=0)
                {   // 覆盖
                    Log.d(TAG,"cover");
                    if(g_u8tAttrTable[g_u8ButtonID][2] >g_u8ActionTblLen)
                    {   // 原有空间 大于 现在空间
                        g_u8tAttrTable[g_u8ButtonID][0] = g_u8TempAttrTable[0];
                        g_u8tAttrTable[g_u8ButtonID][2] = g_u8ActionTblLen;
                        System.arraycopy(g_u8TempAutoEventTbl, 0, g_u8tAutoEventTbl, g_u8tAttrTable[g_u8ButtonID][1], g_u8ActionTblLen);
                        //g_u8AutoEventTblUsed+=g_u8ActionTblLen;
                    }
                    else {
                        g_u8tAttrTable[g_u8ButtonID][0] = 0;
                        reassignedAEVTbl();
                        if (g_u8ActionTblLen + g_u8AutoEventTblUsed > Transfer_Con.MAX_AUTOEVENT_LENGTH) {
                            Toast.makeText(BtnFuncSetting.this,"总共能键长度超出范围，请去除部分无用的功能",Toast.LENGTH_SHORT).show();
                        } else {
                            g_u8tAttrTable[g_u8ButtonID][0] = g_u8TempAttrTable[0];
                            g_u8tAttrTable[g_u8ButtonID][1] = g_u8AutoEventTblUsed;
                            g_u8tAttrTable[g_u8ButtonID][2] = g_u8ActionTblLen;
                            System.arraycopy(g_u8TempAutoEventTbl, 0, g_u8tAutoEventTbl, g_u8AutoEventTblUsed, g_u8ActionTblLen);
                            g_u8AutoEventTblUsed+=g_u8ActionTblLen;
                        }
                    }
                }
                else
                {   // new
                    Log.d(TAG,"new");
                    if(g_u8ActionTblLen+g_u8AutoEventTblUsed>Transfer_Con.MAX_AUTOEVENT_LENGTH)
                    {
                        Toast.makeText(BtnFuncSetting.this,"总共能键长度超出范围，请去除部分无用的功能",Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        g_u8tAttrTable[g_u8ButtonID][0] = g_u8TempAttrTable[0];
                        g_u8tAttrTable[g_u8ButtonID][1] = g_u8AutoEventTblUsed;
                        g_u8tAttrTable[g_u8ButtonID][2] = g_u8ActionTblLen;
                        System.arraycopy(g_u8TempAutoEventTbl,0,g_u8tAutoEventTbl,g_u8AutoEventTblUsed,g_u8ActionTblLen);
                        g_u8AutoEventTblUsed+=g_u8ActionTblLen;
                    }
                }
                String str="";
                for(int a=0;a<g_u8AutoEventTblUsed;a++)
                    str+= String.valueOf((int)g_u8tAutoEventTbl[a]&0xff)+",";
                Log.d(TAG,"Total: "+str);
                Log.d(TAG,"lenth: "+String.valueOf(g_u8AutoEventTblUsed&0xff));
                str="";
                for(int a=0;a<g_u8ActionTblLen;a++)
                    str+= String.valueOf((int)g_u8TempAutoEventTbl[a])+", ";
                Log.d(TAG,"Ev: "+String.valueOf(g_u8tAttrTable[g_u8ButtonID][0]&0xff)+" start: "+String.valueOf(g_u8tAttrTable[g_u8ButtonID][1]&0xff)+" len: "+String.valueOf(g_u8tAttrTable[g_u8ButtonID][2])+" tbl: "+str);


                // 写回
                for(int a=0;a<Transfer_Con.MAX_AUTOEVENT_LENGTH;a+=30) {
                    System.arraycopy(g_u8tAutoEventTbl,a,g_u8Databuf,0,(a+30>=Transfer_Con.MAX_AUTOEVENT_LENGTH)?(Transfer_Con.MAX_AUTOEVENT_LENGTH-a):30);
                    str="";
                    for(int t=0;t<((a+30>=Transfer_Con.MAX_AUTOEVENT_LENGTH)?(Transfer_Con.MAX_AUTOEVENT_LENGTH-a):30);t++)
                        str+= String.valueOf((int)g_u8Databuf[t])+",";
                    Log.d(TAG,"Total: "+str);

                    if(mGlobalVar.tranSet_MultiValue(Transfer_Con.HID_CMD_SET_AEV_TBL, a, (a+30>=Transfer_Con.MAX_AUTOEVENT_LENGTH)?(Transfer_Con.MAX_AUTOEVENT_LENGTH-a):30, g_u8Databuf))
                    {
                       // Log.d(TAG,"Aet write back ok: ");
                    }
                }
                for(int a=0;a<3;a++) {
                    for(int k=0;k<mGlobalVar.getDeviceMaxButton();k++)
                    {
                        g_u8Databuf[k] = g_u8tAttrTable[k][a];
                    }

                    if(mGlobalVar.tranSet_MultiValue(Transfer_Con.HID_CMD_SET_KEY_ATTR, a, mGlobalVar.getDeviceMaxButton(), g_u8Databuf))
                    {
                        //Log.d(TAG,"attr write back ok: ");
                    }
                }
                if (mGlobalVar.tranSet_Value(Transfer_Con.HID_CMD_SAVE_DATA,0,0))
                {
                    Log.d(TAG, "Aet write back ok: ");
                }

            }

        });
        btn_func_combin_reset = (Button)findViewById(R.id.btn_func_combin_reset);
        btn_func_combin_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                g_u8ActionTblLen=0;
                tv_func_combin_view.setText("无");
            }
        });
    }

    void parseAutoEvent()   //连招
    {
        String str ="";
        if(g_u8tAutoEventTbl[g_u8TempAttrTable[1]]==(byte)0xFF)
            return;
        for(int a=0;a<g_u8TempAttrTable[2];a+=2) {
            if ((g_u8tAutoEventTbl[g_u8TempAttrTable[1]+a] & 0x80) == 0x80) {
                str += Transfer_Con.tButtonName[g_u8tAutoEventTbl[g_u8TempAttrTable[1]+a] & 0x3F] + "按";
            }
            else
            {
                str += Transfer_Con.tButtonName[g_u8tAutoEventTbl[g_u8TempAttrTable[1]+a] & 0x3F] + "松";
            }
            if(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+a+1]==(byte)0xFF)
            {
               break;
            }
            if(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+a+1]==0)
            {
                str += ", 同时";
            }
            else
            {
                str += ", 等"+String.valueOf(g_u8tAutoEventTbl[g_u8TempAttrTable[1]+a+1]*100)+"ms, ";
            }
        }
        tv_func_combin_view.setText(str);
    }

    void reassignedAEVTbl()
    {
        byte [] u8Temp=new byte[Transfer_Con.MAX_AUTOEVENT_LENGTH];
        int u16StartAddr = 0;
        Log.d(TAG,"reassignedAEVTbl");
        for(int a=0;a<mGlobalVar.getDeviceMaxButton();a++)
        {
            if( g_u8tAttrTable[a][0]!=0) // 非无效
            {
                Log.d(TAG,"move");
                //转移到暂存队列
                System.arraycopy(g_u8tAutoEventTbl,g_u8tAttrTable[a][1],u8Temp,u16StartAddr,g_u8tAttrTable[a][2]);
                //改变起始地址
                g_u8tAttrTable[a][1]=(byte)u16StartAddr;
                //下一个开始地址
                u16StartAddr+= g_u8tAttrTable[a][2];
            }
        }
        // 存放回数组
        System.arraycopy(u8Temp,0,g_u8tAutoEventTbl,0,u16StartAddr);
        //改变长度
        g_u8AutoEventTblUsed = (byte)u16StartAddr;
    }
}
